package es.udc.pfc.davidparedes.web.pages.user;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.storeservice.StoreService;
import es.udc.pfc.davidparedes.model.universe.Universe;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.DIRECTOR)
public class DeleteUniverse {
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private StoreService storeService;

    @Property
    private Long universeId;
    
    @Property
    private String universeName;
    
    @Component
    private Form deleteUniverseForm;
    
    void onActivate(Long id) throws InstanceNotFoundException {
    	Universe universe = storeService.findUniverse(id);
    	universeId = id;
    	universeName = universe.getName();
    	
    }

    Object onValidateFromDeleteUniverseForm() throws NumberFormatException, InstanceNotFoundException {

        if (!deleteUniverseForm.isValid()) {
            return this;
        }
        
        Universe universe = storeService.findUniverse(universeId);
        
        storeService.deleteUniverse(universe.getUniverseId());
        
        return UniverseSelection.class;
    }

}
